package com.magic.himalaya.views;

import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.magic.himalaya.R;
import com.magic.himalaya.base.BaseApplication;

/**
 * @description:
 * @create: Create by Magic on 2022/1/21.
 */
public abstract class LoaderView extends FrameLayout {

    private View loadingView;
    private View successView;
    private View networkErrorView;
    private View emptyView;

    private OnGetRecommendData recommendData = null;
    private OnGetTrackData trackData = null;

    public enum UIStatus {
        NONE, LOADING, SUCCESS, NETWORK_ERROR, EMPTY
    }

    private static final String TAG = LoaderView.class.getSimpleName();

    public UIStatus mCurrentStatus = UIStatus.NONE;


    public LoaderView(@NonNull Context context) {
        this(context, null); //转到唯一入口
    }

    public LoaderView(@NonNull Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0); //转到唯一入口
    }

    public LoaderView(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);    //使用此唯一入口
        init();
    }

    public void updateStatus(UIStatus status) {
        mCurrentStatus = status;
        //更新UI
        BaseApplication.getHandler().post(() -> switchUIByCurrentStatus());
    }

    /**
     * 初始化UI
     */
    private void init() {
        switchUIByCurrentStatus();
    }

    private void switchUIByCurrentStatus() {
        //加载中
        if (loadingView == null) {
            loadingView = getLoadingView();
            addView(loadingView);
        }
        //根据状态显示是否可见
        loadingView.setVisibility(mCurrentStatus == UIStatus.LOADING ? VISIBLE : GONE);

        //成功
        if (successView == null) {
            successView = getSuccessView(this);
            addView(successView);
        }
        //根据状态显示是否可见
        successView.setVisibility(mCurrentStatus == UIStatus.SUCCESS ? VISIBLE : GONE);

        //网络错误
        if (networkErrorView == null) {
            networkErrorView = getNetworkErrorView();
            addView(networkErrorView);
        }
        //根据状态显示是否可见
        networkErrorView.setVisibility(mCurrentStatus == UIStatus.NETWORK_ERROR ? VISIBLE : GONE);

        //空数据
        if (emptyView == null) {
            emptyView = getEmptyView();
            addView(emptyView);
        }
        //根据状态显示是否可见
        emptyView.setVisibility(mCurrentStatus == UIStatus.EMPTY ? VISIBLE : GONE);

    }

    private View getEmptyView() {
        return LayoutInflater.from(getContext()).inflate(R.layout.fragment_recommend_empty, this, false);
    }

    private View getNetworkErrorView() {
        View view = LayoutInflater.from(getContext()).inflate(R.layout.fragment_recommend_networkerror, this, false);
        View error_icon = view.findViewById(R.id.network_error_icon);

        //网络出错时点击图标进行重新加载
        error_icon.setOnClickListener(v -> {
            if (recommendData != null) {    //如果是推荐页面加载出错，重新获取推荐数据
                recommendData.onGetRecommendList();
            }
            if (trackData != null) {    //如果是声音页面加载出错，重新获取声音数据
                trackData.onGetTrackList();
            }
        });
        return view;
    }

    private View getLoadingView() {
        return LayoutInflater.from(getContext()).inflate(R.layout.fragment_recommend_loading, this, false);
    }

    public abstract View getSuccessView(ViewGroup container);


    public interface OnGetTrackData {
        void onGetTrackList();
    }

    public void setTrackData(OnGetTrackData trackData) {
        this.trackData = trackData;
    }


    public interface OnGetRecommendData {
        void onGetRecommendList();
    }

    public void setRecommendData(OnGetRecommendData recommendData) {
        this.recommendData = recommendData;
    }
}
